package com.iplant.wms.serviceImpl.dao.brm;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.po.brm.BRMSampleProp;
import com.iplant.wms.service.po.brm.BRMSampleRange;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
@DBBaseDAO(BRMSampleProp.class)
public class BRMSamplePropDAO extends BaseDAO {

    private static BRMSamplePropDAO Instance = null;

    private BRMSamplePropDAO() {
        super("");
        this.Init();
    }

    public static BRMSamplePropDAO getInstance() {
        if (Instance == null) {
            Instance = new BRMSamplePropDAO();
        }
        return Instance;
    }





    public void BRM_DeleteSampleProp(BMSEmployee wLoginUser, int wRuleID, OutResult<Integer> wErrorCode) {
        wErrorCode.set(0);

        try {
            if (wRuleID <= 0) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }
            Map<String, Object> wDeleteParams = new HashMap<>();
            wDeleteParams.put("RuleID", wRuleID);
            this.Delete(wDeleteParams, " and ID>0 ");

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            log.error("BRM_DeleteSampleProp", ex);
        }
    }


    public List<BRMSampleProp> BRM_QuerySamplePropList(
            BMSEmployee wLoginUser, int wRuleID, String wName, String wCode, int wASNTypeID, int wActive,
            Pagination wPagination, OutResult<Integer> wErrorCode) {
        Map<String, Object> wParams = new HashMap<>();

        wParams.put("Name", wName);
        wParams.put("RuleID", wRuleID);
        wParams.put("Code", wCode);
        wParams.put("ASNTypeID", wASNTypeID);
        wParams.put("Active", wActive);

        return this.SelectPOList(wLoginUser, wParams, wPagination, wErrorCode);
    }


    public BRMSampleProp BRM_QuerySampleProp(
            BMSEmployee wLoginUser, String wMaterialNo, int wASNTypeID, double wMaterialNum,
            OutResult<Integer> wErrorCode) {
        BRMSampleProp wResult = new BRMSampleProp();
        try {
            if (StringUtils.isEmpty(wMaterialNo) || wMaterialNum <= 0) {
                return wResult;
            }
            BRMSampleRange brmSampleRange = BRMSampleRangeDAO.getInstance().BRM_QuerySampleRange(wLoginUser,
                    wMaterialNo, wErrorCode);

            if (wErrorCode.Result != 0 || brmSampleRange == null || brmSampleRange.ID <= 0 ||
                    brmSampleRange.RuleID <= 0 || StringUtils.isEmpty(brmSampleRange.RuleCode)) {
                return wResult;
            }

            return this.BRM_QuerySampleProp(
                    wLoginUser, brmSampleRange.RuleID, "", wASNTypeID, wMaterialNum, wErrorCode);

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            log.error("BRM_QuerySampleProp", ex);
        }
        return wResult;

    }

    public BRMSampleProp BRM_QuerySampleProp(
            BMSEmployee wLoginUser, int wRuleID, String wCode, int wASNTypeID, double wMaterialNum,
            OutResult<Integer> wErrorCode) {
        BRMSampleProp wResult = new BRMSampleProp();
        try {

            if (wMaterialNum <= 0) {
                return wResult;
            }
            if (wRuleID <= 0 && StringUtils.isEmpty(wCode)) {
                return wResult;
            }
            Pagination wPagination = Pagination.Create(1, Integer.MAX_VALUE, "ID", "ASC");
            List<BRMSampleProp> brmSampleProps = BRMSamplePropDAO.getInstance().BRM_QuerySamplePropList(wLoginUser,
                    wRuleID, "", wCode, wASNTypeID, 1, wPagination, wErrorCode);

            if (wErrorCode.Result != 0 || brmSampleProps == null || brmSampleProps.size() <= 0) {
                return wResult;
            }

            for (BRMSampleProp brmSampleProp : brmSampleProps) {
                if (brmSampleProp.SampleNumber <= 0 && brmSampleProp.SampleRate <= 0) {
                    continue;
                }
                if (brmSampleProp.MinNumber <= wMaterialNum && brmSampleProp.MaxNumber >= wMaterialNum) {
                    wResult = brmSampleProp;
                    break;
                }
            }


        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            log.error("BRM_QuerySampleProp", ex);
        }
        return wResult;

    }




}
